package com.ssy.invoicing.suppliers.controller;

import java.sql.SQLException;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ssy.invoicing.common.controller.BaseController;
import com.ssy.invoicing.dao.support.BaseSqlDao.SplitPageResult;
import com.ssy.invoicing.suppliers.bean.SuppliersBean;
import com.ssy.invoicing.suppliers.service.SuppliersService;

/**
 * 供应商管理Controller
 * Comment of SuppliersController
 * @author suoyao.shen
 */
@Controller
@RequestMapping("invoicing")
public class SuppliersController extends BaseController {
    
    Logger logger = LoggerFactory.getLogger(SuppliersController.class);
    
    @Resource(name="suppliersService")
    private SuppliersService suppliersService;

    /**
     * 新增供应商信息
     * @author suoyao.shen
     * @return
     */
    @RequestMapping("addSuppliers")
    public ModelAndView addSuppliers() {
        return new ModelAndView("pages/suppliers/add");
    }
    
    /**
     * 查询客户信息
     * @author suoyao.shen
     * @param model
     * @return
     */
    @RequestMapping("listSuppliers")
    public ModelAndView listCustomer(Integer page, ModelMap model) {
        try {
            SplitPageResult spr = suppliersService.querySuppliersListByPage(page);
            model.put("pagination", spr.getPagination());
            model.put("suppliersList", spr == null ? null : spr.getResult());
        } catch (SQLException e) {
            logger.error("query suppliers list fail.",e);
            return new ModelAndView("pages/error/err");
        }
        return new ModelAndView("pages/suppliers/list");
    }
    
    /**
     * 获得用户详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("editSuppliers")
    public ModelAndView editCustomer(Integer id,ModelMap model) {
        SuppliersBean suppliers = suppliersService.getSuppliers(id);
        model.put("suppliers", suppliers);
        return new ModelAndView("pages/suppliers/edit");
    }
    
    /**
     * 删除用户信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("removeSuppliers")
    public ModelAndView removeCustomer(Integer id,ModelMap model) {
        suppliersService.removeSuppliers(id);
        return new ModelAndView("redirect:listSuppliers.htm");
    }
    
    /**
     * 保存客户信息
     * @author suoyao.shen
     * @param customer
     * @param model
     * @return
     */
    @RequestMapping("saveSuppliers")
    public ModelAndView saveCustomer(SuppliersBean suppliers, ModelMap model) {
        suppliersService.saveSuppliers(suppliers);
        return new ModelAndView("redirect:listSuppliers.htm");
    }
    
    /**
     * 更新客户信息
     * @param customer
     * @param model
     * @return
     */
    @RequestMapping("updateSuppliers")
    public ModelAndView updateCustomer(SuppliersBean suppliers, ModelMap model) {
        suppliers.setAddTime(new Date());
        suppliersService.updateSuppliers(suppliers);
        return new ModelAndView("redirect:listSuppliers.htm");
    }
    
}
